<?php
namespace app\api\controller\wsgoods;

class Goods extends Base
{


    /**
     * 商品列表
     */
    public function lists()
    {
        $d = $this->post();
        $rule = [
            'page' => 'require|integer',
            'limit' => 'require|integer',
        ];
        $msg = [

        ];
        check($rule, $msg, $d);
        $map = [];
        $map[] = ['and', 'status', '=', 0];
        if ($d["cat_id"] != '') {
            $ids = m("GoodsCat", "c")->getAllchildCat($d["cat_id"]);
            $ids[] = $d['cat_id'];
            if ($ids) {
                $map[] = ['and', 'cat_id', 'in', $ids];
            }
        }
        if ($d["keyword"] != '') $map[] = ["and", "title", "like", '%' . $d["keyword"] . '%'];
        if ($d["rec_id"] != '') $map[] = ["and", "rec_id", "like", '%,' . $d['rec_id'] . ',%'];
        $w = where($map);
        $order = '';
        switch ($d['order']) {
            case 1: //综合排序
                $order = "order by sort desc,id desc";
                break;
            case 2: //销量排序
                $order = "order by sales_volume desc,sort desc";
                break;
            case 3: //人气排序
                $order = "order by views desc,sales_volume desc";
                break;
            case 4://价格升序
                $order = "order by price asc,views desc";
                break;
            case 5://价格降序
                $order = "order by price desc,views desc";
                break;
            case 6://最新降序
                $order = "order by create_time desc";
                break;
            case 7://最新升序
                $order = "order by create_time desc";
                break;
            default:
                $order = "order by sort desc,id desc";
        }
        $field = ['id','title', 'price', 'pic', 'point', 'sales_volume','goods_price_id','description'];
        $field = implode(",", $field);
        $sql = "select {$field} from goods  $w[0] {$order} " . limit($d['page'], $d['limit']);
        $rs = $this->db->query($sql, $w[1], '', true);
        suc($rs[0], $rs[1]);
    }

    //商品详情
    public function detail()
    {
        $d = $this->post();
        $rule = [
            'goods_price_id' => 'require',
        ];
        $msg = [
        ];
        check($rule, $msg, $d);
        //产品信息
        $info = m('goods', 'c')->detail($d['goods_price_id']);
        if (!$info) err('没有找到产品信息');
        $info['spec'] = htmlspecialchars_decode($info['spec']);
        $info['content'] = htmlspecialchars_decode($info['content']);
        if ($info['pics']) {
            $info['pics'] = explode(",", $info["pics"]);
        }
        //属性
        $attr = [];
        $temp = [];
        if ($info['spec']) {
            $spec = json_decode($info['spec'], true);
            $ptitle = explode("_", $info['p_title']);
            $i = 0;
            foreach ($spec as $k => $v) {
                $arr = [
                    'name' => $k,
                    'lists' => [],
                ];
                foreach ($v as $key => $val) {
                    $arr_1 = [];
                    $arr_1['title'] = $val;
                    $arr_1['checked'] = $val == $ptitle[$i] ? 1 : 0;
                    $arr['lists'][] = $arr_1;
                }
                $temp[] = $arr;
                $i++;
            }
        }
        $info['h5_url'] = url('h5/goodsdetail', ['id' => $info['id']]);
        $info['spec'] = $temp;

        //人气累加
        $data = [
            "views" => $info['p_views'] + 1,
            "id" => $info['id']
        ];
        $this->db->update("goods_price", $data);
        //添加浏览记录
        if(isset($this->userinfo["id"])){
            $rs = $this->db->query("select * from goods_history where tab_id=? and user_id=?",[$info['id'],$this->userinfo["id"]]);
            if(!$rs){
                $data = [
                    'tab_id' => $info['id'],
                    'user_id' => $this->userinfo["id"],
                    'create_time' => time(),
                ];
                $this->db->insert('goods_history',$data);
            }
        }

        //是否收藏
        $iscollect = 0;
        if ($this->userinfo) {
            $collectinfo = $this->db->query('select * from user_collect where tab_id=? and user_id=?', [$info['id'], $this->userinfo["id"]]);
            if ($collectinfo) {
                $iscollect = 1;
            }
        }
        $info['iscollect'] = $iscollect;
        suc($info);

    }

    //查找规格商品
    public function findGoods(){
        $d = $this->post();
        $rule = [
            'title'=>'require',
            'goods_id'=>'require|integer',
        ];
        $msg = [
        ];
        check($rule,$msg,$d);
        $info = $this->db->query('select * from goods_price where title = ? and goods_id =?',[$d['title'],$d['goods_id']],1);
        suc($info);
    }
}

?>
